// let _UrlGet_ = wx.getStorageSync('wx_url')
let _UrlGet_ = wx.getStorageSync('wx_url')
let _baseUrlGet_ = wx.getStorageSync('img_url')

module.exports = {
  requestUrl: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Authorization': getApp().globalData.token
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.code > 0) {
          callback.apply(pageobj, [res.data])
        } else {
          var error_msg = 'error:接口请求错误';
          if (res.data.info != 'null' || res.data.info != '') {
            error_msg = res.data.info
          }
          wx.showModal({
            title: '提示',
            content: error_msg,
            showCancel: false
          })
        }
      },
      complete: function () { }
    })
  },
  httpRequest: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Authorization': getApp().globalData.token
      },
      success: function (res) {
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: "error:接口请求错误",
            showCancel: false
          })
        } else {
          callback.apply(pageobj, [res.data]);
          if (res.data.code != 2 && res.data.code != 1) {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false
            })
          }
        }
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: "error:网络请求失败",
          showCancel: false
        })
      }
    })
  },
  // 全部返回 post
  Request: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'post',
      header: {
        'Authorization': getApp().globalData.token
      },

      success: callback,
      fail: function (res) {
        wx.showModal({
          title: '错误提示',
          content: res.ms,
          showCancel: false
        })
      },
      complete: function (res) {
      }
    })
  },
  // 全部返回 get
  // RequestGet: function (data, url, callback, pageobj) {

  //   let params = ``;
  //   for (let key in data) {
  //     if (data[key] != null && data[key] != '') {
  //       if (params.length == 0) {
  //         params += `?${key}=${data[key]}`;
  //       } else {
  //         params += `&${key}=${data[key]}`;
  //       }
  //     }
  //   }
  //   url += params;

  //   wx.request({
  //     url: url,
  //     method: 'get',
  //     header: {
  //       'Authorization': getApp().globalData.token
  //     },
  //     success: callback,
  //     fail: function (res) {
  //       wx.showModal({
  //         title: '错误提示',
  //         content: res.ms,
  //         showCancel: false
  //       })
  //     },
  //     complete: function (res) {
  //     }
  //   })
  // },
  RequestGet: function (data, url, callback, pageobj) {
    // 拼接查询参数
    let params = "";
    for (let key in data) {
      if (data[key] != null && data[key] != "") {
        if (params.length === 0) {
          params += `?${key}=${encodeURIComponent(data[key])}`; // 使用 encodeURIComponent 避免特殊字符问题
        } else {
          params += `&${key}=${encodeURIComponent(data[key])}`;
        }
      }
    }

    // 确保 URL 拼接正确，移除多余的空格
    url = url.trim() + params;

    wx.request({
      url: url,
      method: "GET",
      header: {
        "Authorization": getApp().globalData.token
      },
      success: function (res) {
        if (callback && typeof callback === "function") {
          callback(res, pageobj);
        }
      },
      fail: function (res) {
        wx.showModal({
          title: "错误提示",
          content: res.errMsg || "请求失败，请检查网络或重试",
          showCancel: false
        });
      }
    });
  },
  // PUT 请求
  put: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'PUT',
      header: {
        'Authorization': getApp().globalData.token,
        'Content-Type': 'application/json' // 根据后端要求设置
      },
      success: callback,
      fail: function (res) {
        wx.showModal({
          title: '错误提示',
          content: res.errMsg || '网络请求失败',
          showCancel: false
        });
      },
      complete: function (res) {
        console.log('PUT 请求完成:', res);
      }
    });
  },

  // DELETE 请求
  delete: function (data, url, callback, pageobj) {
    wx.request({
      url: url,
      data: data,
      method: 'DELETE',
      header: {
        'Authorization': getApp().globalData.token,
        'Content-Type': 'application/json' // 根据后端要求设置
      },
      success: callback,
      fail: function (res) {
        wx.showModal({
          title: '错误提示',
          content: res.errMsg || '网络请求失败',
          showCancel: false
        });
      },
      complete: function (res) {
        console.log('DELETE 请求完成:', res);
      }
    });
  },

  // 注册登陆
  postwxappLogin: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/user/login', callback, pageobj)
  },

  // 小程序-授权 头像 昵称 姓名 电话
  postLogin: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/api/applet/login', callback, pageobj)
  },

  // 小程序-授权手机号
  postGetUserMobile: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/api/applet/getUserMobile', callback, pageobj)
  },

  // 地区
  getTreeArea: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/api/applet/treeArea', callback, pageobj)
  },




  // 热门职业
  getJobTitle: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _baseUrlGet_ + '/dev-api/system/dict/data/type/teacher_job_title', callback, pageobj)
  },
  // 热门职业
  getJobType: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _baseUrlGet_ + '/dev-api/system/dict/data/type/job_type', callback, pageobj)
  },  // 热门职业
  getJobCategory: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _baseUrlGet_ + '/dev-api/system/dict/data/type/job_category', callback, pageobj)
  },

  // http://localhost/dev-api/system/dict/data/type/teacher_job_title
  // http://localhost/dev-api/system/dict/data/type/job_type
  // http://localhost/dev-api/system/dict/data/type/job_category
  // 热门职业
  getHotJob: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/job/listByScore', callback, pageobj)
  },
  // 热门导师
  getHotTeacher: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/teacher/listByNumber', callback, pageobj)
  },
  // 职业列表
  getJobList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/job/list', callback, pageobj)
  },
  // 导师列表
  getTeacherList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/teacher/list', callback, pageobj)
  },

  // 职业评分
  // id 学生ID 
  // socre
  postUpdateScore: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/job/updateScore', callback, pageobj)
  },
  // 学生预约
  //  studentId 学生ID 
  // teacherId 教师ID 
  // bookingTime 预约时间
  postBooking: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/booking', callback, pageobj)
  },
  // 学生预约列表
  //   /wx/booking/bookStudent GET
  // 传字段teacherId  和分页查询一样的传递方式（对象）
  getBookStudentList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/booking/bookStudent ', callback, pageobj)
  },
  // 教师是否接受预约
  // id 预约id 
  // isAgree 是否同意（”N“否，”Y“是） 
  putBooking: function (data, callback, pageobj) {
    let that = this
    let res = that.put(data, _UrlGet_ + '/booking', callback, pageobj)
  },
  // 添加进消息列表
  // teacherId 当前登录id（点击同意预约的教师id） 
  // studentId（发起预约的学生id）
  // startTime (会话开启时间)
  postMesList: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/list/add', callback, pageobj)
  },
  // 发送消息
  // chatID 对话id db_chat_list的id
  // senderId 发言者Id
  // speakUser发言对象身份
  // receiverId 接收者Id
  // content 内容
  // parentId 上一条信息id（如果能获取的话）
  postSendMessage: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/chat/sendMessage', callback, pageobj)
  },
  // 获取聊天列表
  getMessageList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/list/list', callback, pageobj)
  },
  // 获取聊天记录
  // 查询条件是会话db_chat_list的id 对应chatId；
  // 根据当前用户的身份，0：教师；1：学生 ，将对话记录展示，类型字段为speak_user
  getRecordList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/record/listPage ', callback, pageobj)
  },
  // 获取学习视频
  getVideoList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/vedio/list ', callback, pageobj)
  },
  // 添加学习视频
  // name 视频名称
  // introduce 学习目标
  // isPublish 是否发布（‘Y’, 'N'） 默认是
  // duration 时长
  // url 
  // uploadId 上传人ID
  putAddVideo: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/vedio', callback, pageobj)
  },
  // 修改学习视频
  // putEdiVideo: function (data, callback, pageobj) {
  //   let that = this
  //   let res = that.put(data, _UrlGet_ + '/vedio', callback, pageobj)
  // },
  // 获取学习视频详情
  getVideoDetail: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/vedio/${id}`, callback, pageobj)
  },


  // 获取学习讲座列表
  // /wx/lecture/list
  // 除了查询字段还需要当前登录学生id
  // studentId
  // 会多返回一个字段status  0：未加入，1：已加入
  getLectureList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + '/lecture/list ', callback, pageobj)
  },
  // 添加学习讲座列表
  // name 名称
  // content 讲座内容
  // presenter 主讲人
  // startTime 开始时间
  // endTime 结束时间
  // address 举办地点
  // participantsNumber 是否结束(0:未开始；1：进行中；2：已结束)默认未开始
  putAddLecture: function (data, callback, pageobj) {
    let that = this
    let res = that.put(data, _UrlGet_ + '/lecture', callback, pageobj)
  },
  // 修改学习讲座列表
  // putEdiVideo: function (data, callback, pageobj) {
  //   let that = this
  //   let res = that.put(data, _UrlGet_ + '/vedio', callback, pageobj)
  // },
  // 获取学习讲座列表
  getLectureDetail: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/lecture/${id}`, callback, pageobj)
  },

  // 讲座预约
  // lectureId 讲座id
  // studentId 学生id
  postLectureBooking: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/lectureStudent/booking', callback, pageobj)
  },
  // 发布论坛
  // title 标题
  // type 发布人身份（0：学生；1：教师）
  // content 内容
  // createBy 发布者id
  postLearnLists: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/learnLists', callback, pageobj)
  },
  // 获取论坛列表
  getLearnLists: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/learnLists/list`, callback, pageobj)
  },
  // 修改论坛
  putLearnLists: function (data, callback, pageobj) {
    let that = this
    let res = that.put(data, _UrlGet_ + '/learnLists', callback, pageobj)
  },
  // 删除论坛
  DeleteLearnLists: function (ids, data, callback, pageobj) {
    let that = this
    let res = that.delete(data, _UrlGet_ + `/learnLists/${ids}`, callback, pageobj)
  },

  // 发布评论
  // titleId 主贴ID
  // content 内容
  // createBy 评论人ID
  // type 评论人类型（0：学生；1：教师）
  postComment: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/comment', callback, pageobj)
  },
  // 获取评论列表
  getCommentList: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/comment/list?titleId=${id}`, callback, pageobj)
  },
  // 修改评论
  putComment: function (data, callback, pageobj) {
    let that = this
    let res = that.put(data, _UrlGet_ + '/comment', callback, pageobj)
  },
  // 删除评论
  DeleteComment: function (ids, data, callback, pageobj) {
    let that = this
    let res = that.delete(data, _UrlGet_ + `/comment/${ids}`, callback, pageobj)
  },


  // 获取学生预约列表
  getStudentBookList: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/booking/bookStudent?titleId=${id}`, callback, pageobj)
  },
  // 社团列表
  // studentId 
  // societyId
  getSocietyList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/societies/list`, callback, pageobj)
  },
  // 社团详情
  getSocietyDetail: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/societies/${id}`, callback, pageobj)
  },
  // 学生加入社团
  // studentId 学生id
  // societyId 社团id
  postStudentSociety: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/studentSociety', callback, pageobj)
  },
  // 退出社团
  // studentId 
  // societyId
  exitSocietyComment: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + `studentSociety/exitSocietyexitSociety`, callback, pageobj)
  },
  // 社团活动列表
  // studentId 
  // societyId
  getActivitiesList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/activities/list`, callback, pageobj)
  },
  // 某一个社团的全部社团活动
  getSocietyActivitiesList: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/activities/list?societyId=${id}`, callback, pageobj)
  },
  // 社团活动详情
  getActivitiesDetail: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/activities/${id}`, callback, pageobj)
  },
  // 学生加入社团活动
  // studentId 学生id
  // activityId 活动id
  postStudentActivity: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + '/studentActivity', callback, pageobj)
  },
  // 退出社团活动
  // studentId 
  // activityId
  exitActivityComment: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + `/studentActivity/exitActivity`, callback, pageobj)
  },
  // 获取学生计划列表
  getplanList: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/planning/list?createBy=${id}`, callback, pageobj)
  },
  // 添加学习计划
  // title 标题
  // content 内容
  // planDays 计划完成天数
  // createBy 创建者id
  postPlanning: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + `/planning`, callback, pageobj)
  },
  // 获取学生计划详情
  getplanDetail: function (id, data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/planning/${id}`, callback, pageobj)
  },
  // 修改学习计划
  putplanning: function (data, callback, pageobj) {
    let that = this
    let res = that.put(data, _UrlGet_ + '/planning', callback, pageobj)
  },
  // 删除学习计划
  DelPlaning: function (ids, data, callback, pageobj) {
    let that = this
    let res = that.delete(data, _UrlGet_ + `/planning/${ids}`, callback, pageobj)
  },
  // 学习计划打卡
  postPlanningClock: function (data, callback, pageobj) {
    let that = this
    let res = that.Request(data, _UrlGet_ + `/planning/clock`, callback, pageobj)
  },
  // 学生加入的社团列表
  getStudentSocietyList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/studentSociety/joinSocieties`, callback, pageobj)
  },
  // 学生参加的社团活动列表
  getStudentActivityList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/studentActivity/joinActivities`, callback, pageobj)
  },
  // 查询当前学生预约老师的记录
  getBookTeacherList: function (data, callback, pageobj) {
    let that = this
    let res = that.RequestGet(data, _UrlGet_ + `/booking/bookTeacher`, callback, pageobj)
  },

  // http://localhost/dev-api/common/upload
}